//
//  ZYImageViewController.m
//  Project_B
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "ZYImageViewController.h"

@interface ZYImageViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *myScrollView;
@property(nonatomic,strong)UIScrollView *contentSV;
@property(nonatomic,strong)UIImageView *imgView;
@property(nonatomic,strong)UILabel *titleLb;
@property(nonatomic,strong)UILabel *contentLb;
@property(nonatomic,strong)UILabel *replyLb;
@property(nonatomic,strong)UILabel *page;

@property(nonatomic,strong)NSMutableArray *photosArr;

@end

@implementation ZYImageViewController

#pragma mark - 将要出现时
-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = YES;
}

#pragma mark - 将要消失时
-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;

}


#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blackColor];
    self.photosArr = [NSMutableArray array];
    
    
    
    //创建ScrollView
    self.myScrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.myScrollView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.myScrollView];
    self.myScrollView.delegate = self;
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.bounces = NO;
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    
    
    //返回左按钮
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:leftBtn];
    [leftBtn setImage:[UIImage imageNamed:@"iconfont-fanhui-bai.png"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.top.mas_equalTo(SCREEN_WIDTH/20);
        make.width.mas_equalTo(30*ADAPT_WIDTH);
        make.height.mas_equalTo(30*ADAPT_HEIGHT);
    }];
    
    
    //跟帖数
    _replyLb = [[UILabel alloc]init];
    _replyLb.text = [NSString stringWithFormat:@"%@跟帖", _replyStr];
    _replyLb.textColor = [UIColor whiteColor];
    _replyLb.font = [UIFont systemFontOfSize:13*ADAPT_FONT];
    _replyLb.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:_replyLb];
    
    [_replyLb mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-SCREEN_WIDTH/15);
        make.top.mas_equalTo(SCREEN_WIDTH/8);
        make.width.mas_equalTo(80*ADAPT_WIDTH);
        make.height.mas_equalTo(25 * ADAPT_HEIGHT);
    }];
    
    
    //创建详情scrollView
    self.contentSV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/5*3,SCREEN_WIDTH, SCREEN_HEIGHT/3)];
    self.contentSV.backgroundColor = [UIColor clearColor];
    self.contentSV.alpha = 0.8;
    [self.view addSubview:self.contentSV];
    self.contentSV.bounces = NO;
    
    //view
    UIView *aView = [[UIView alloc]init];
    aView.backgroundColor = [UIColor blackColor];
    aView.alpha = 0.8;
    [self.contentSV addSubview:aView];
    
    [aView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(SCREEN_WIDTH/5);
        make.size.equalTo(self.contentSV);
    }];
    
    
    
    //标题
    self.titleLb = [[UILabel alloc]init];
    [aView addSubview:self.titleLb];
    self.titleLb.font = [UIFont boldSystemFontOfSize:18*ADAPT_FONT];
    self.titleLb.textColor = [UIColor whiteColor];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(SCREEN_WIDTH/30);
        make.left.mas_equalTo(SCREEN_WIDTH/30);
        make.height.mas_equalTo(SCREEN_WIDTH/20);
        make.width.mas_equalTo(SCREEN_WIDTH/3*2);
        
    }];
    
    
    //内容
    self.contentLb = [[UILabel alloc]init];
    self.contentLb.font = [UIFont systemFontOfSize:15*ADAPT_FONT];
    [aView addSubview:_contentLb];
    self.contentLb.textColor = [UIColor whiteColor];
    self.contentLb.numberOfLines = 0;

    
    //页码
    self.page = [[UILabel alloc]init];
    self.page.font = [UIFont boldSystemFontOfSize:18*ADAPT_FONT];
    [aView addSubview:_page];
    self.page.textColor = [UIColor whiteColor];
    self.page.textAlignment = NSTextAlignmentRight;
    
    [_page mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(SCREEN_WIDTH/30);
        make.left.equalTo(self.titleLb.mas_right).offset(SCREEN_WIDTH/20);
        make.height.equalTo(self.titleLb);
        make.width.mas_equalTo(SCREEN_WIDTH/6);
    }];
    
 
    
    [self getData];
}


#pragma mark - 数据请求
-(void)getData{

    NSString *str = [[self.url substringFromIndex:4] stringByReplacingOccurrencesOfString:@"|" withString:@"/"];
   
     NSString *url = [NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/%@.json",str];
    
//    NSLog(@"=========%@",url);
    
    [ZYTools getUrl:url body:nil result:ZYJSON headerFile:nil success:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"photos"];

        for (NSMutableDictionary *dic in array) {
            ZYPhotosModel *model  = [[ZYPhotosModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            model.labelHeight = [LhHeightForContent heightForContent:[NSString stringWithFormat:@"%@,%@",model.imgtitle,model.note] font:16*ADAPT_FONT width:SCREEN_WIDTH];
            
            [self.photosArr addObject:model];
            
        }
        
        
#pragma mark  创建相册
        
        self.myScrollView.contentSize = CGSizeMake(SCREEN_WIDTH*self.photosArr.count, self.view.frame.size.height);
        
        for (int i = 0; i < self.photosArr.count; i++) {
            
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/30 + SCREEN_WIDTH * i, 0, SCREEN_WIDTH - SCREEN_WIDTH/15, self.view.frame.size.height)];

            img.contentMode = UIViewContentModeScaleAspectFit;
            
            ZYPhotosModel *model = [self.photosArr objectAtIndex:i];
            NSURL *url = model.imgurl;
            [img sd_setImageWithURL:url placeholderImage:PLACEHOLDER_IMAGE];
            
            [self.myScrollView addSubview:img];
        }
        
        
       
        //赋值
        NSString *string = [NSString stringWithFormat:@"1/%ld",self.photosArr.count];
        
        NSString *str = [NSString stringWithFormat:@"/%ld", self.photosArr.count]; //是需要变小的文字
        
        // 设置标签文字
        NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:string];
        
        // 获取位置和长度
        NSRange range = [string rangeOfString:str]; //获取需要变量文字的文字
        
        // 设置标签文字的属性
        [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:14.0]} range:range];
        
        // 显示在Label上
        _page.attributedText = attrituteString;

        
        
        
        ZYPhotosModel *model = [self.photosArr objectAtIndex:0];
        //标题
        self.titleLb.text = [result objectForKey:@"setname"];
        //内容
        if (!model.imgtitle) {
            _contentLb.text = [NSString stringWithFormat:@"%@",model.note];
            
        }else{
            _contentLb.text = [NSString stringWithFormat:@"%@ %@",model.imgtitle,model.note];
            
        }
        
        
        //自适应高度
        [_contentLb mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.titleLb);
            make.top.equalTo(self.titleLb.mas_bottom).offset(SCREEN_WIDTH/30);
            make.width.mas_equalTo(SCREEN_WIDTH - SCREEN_WIDTH/15);
            make.height.mas_equalTo(model.labelHeight);
            
        }];
        
        
        self.contentSV.contentSize = CGSizeMake(SCREEN_WIDTH, model.labelHeight + SCREEN_WIDTH/10 + SCREEN_WIDTH/20 +SCREEN_WIDTH/5);
        
        
    } failure:^(NSError *error) {
        
        
    }];



   
}



#pragma mark -  滑动停止时触发
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger num = self.myScrollView.contentOffset.x/SCREEN_WIDTH + 1;
    
    NSString *string = [NSString stringWithFormat:@"%ld/%ld",num, self.photosArr.count];
    NSString *str = [NSString stringWithFormat:@"/%ld", self.photosArr.count]; //是需要变小的文字
    
    // 设置标签文字
    NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:string];
    
    // 获取位置和长度
    NSRange range = [string rangeOfString:str]; //获取需要变量文字的文字
    
    // 设置标签文字的属性
    [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:14.0]} range:range];
    
    // 显示在Label上
    _page.attributedText = attrituteString;

    
    
    
    
    
    ZYPhotosModel *model = [self.photosArr objectAtIndex:num - 1];
    
    //内容
    if (!model.imgtitle) {
        _contentLb.text = [NSString stringWithFormat:@"%@",model.note];
        
    }else{
        _contentLb.text = [NSString stringWithFormat:@"%@ %@",model.imgtitle,model.note];
    }
    
    
    //自适应高度
    [_contentLb mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.titleLb);
        make.top.equalTo(self.titleLb.mas_bottom).offset(SCREEN_WIDTH/30);
        make.width.mas_equalTo(SCREEN_WIDTH - SCREEN_WIDTH/15);
        make.height.mas_equalTo(model.labelHeight);
        
    }];

    
}

#pragma mark - 左按钮
-(void)leftBtnAction{
     self.navigationItem.hidesBackButton = YES;
    [self.navigationController popViewControllerAnimated:YES];
}







#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
